package com.zg.job.quartz;

import com.google.common.collect.Maps;
import com.zg.common.core.dao.system.SysJob;
import com.zg.job.syscode.Constant;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/2/20 17:54
 */
@Slf4j
@Component
public abstract  class AJobLauncher extends QuartzJobBean {

    @Resource
    private JobLauncher jobLauncher;


    public abstract JobExecution executeBusinessLogic(JobLauncher jobLauncher,SysJob job,JobParameters jobParameters);

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String jobName = jobExecutionContext.getJobDetail().getKey().getName();
        log.info("[{}] is running...", jobName);
        SysJob job=(SysJob) jobExecutionContext.getJobDetail().getJobDataMap().get(Constant.DEFAULT_TARGET_SERVER);
        //String collateConfig = jobExecutionContext.getJobDetail().getJobDataMap().get(Constant.DEFAULT_PARAMS_KEY);
        //启动SpringBatch任务
        Map<String, JobParameter> jobParameterMap = Maps.newHashMap();
        //已经转化好，后面直接用，不用再次转换了
        jobParameterMap.put("runTime", new JobParameter(System.currentTimeMillis()));
        JobParameters jobParameters = new JobParameters(jobParameterMap);
//        JobParameters params = new JobParametersBuilder()
//                .toJobParameters();

        JobExecution jobExecution =executeBusinessLogic(jobLauncher,job,jobParameters);

        if (jobExecution != null) {
            if (BatchStatus.COMPLETED != jobExecution.getStatus()) {
                throw new RuntimeException("exception...");
            }
        }

    }

    private Map<String, JobParameter> getJobParameters(SysJob collateConfig) {
        Map<String, JobParameter> parameters = new HashMap<>();
        Field[] fields = collateConfig.getClass().getDeclaredFields();
        if (fields != null && fields.length > 1) {
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    if (field.getType() == String.class) {
                        parameters.put(field.getName(), new JobParameter((String) field.get(collateConfig)));
                    } else if (field.getType() == Long.class) {
                        parameters.put(field.getName(), new JobParameter((Long) field.get(collateConfig)));
                    }
                } catch (IllegalAccessException e) {
                    log.error("{} can not access", field.getName());
                }
            }
        }
        return parameters;
    }
}
